//
//  UIBarButtonItem+Extension.swift
//  Hyy9Weibo
//
//  Created by Air on 16/3/31.
//  Copyright © 2016年 hyy. All rights reserved.
//

import UIKit

extension UIBarButtonItem{
    //扩展里只能添加便利构造函数,便利构造函数必须调用指定构造函数来创建对象
    convenience init(imageName:String,target:AnyObject?,action:Selector) {
        //1.自己调用指定构造函数
//        super.init()
        let button = UIButton()
        button.setImage(UIImage(named: "\(imageName)"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        button.sizeToFit()
        
        //按钮添加点击事件
        button.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        
        //2.也可以便利构造函数，实际上内部调用了指定构造函数
        self.init(customView: button)
    }
    
    /**
     创建一个带按钮的uibarbuttonitem
     
     - parameter imageName: 图片名称
     
     - returns: UIBarButtonItem
     */
//    class func createBarButtonItem(imageName:String) -> UIBarButtonItem {
//        let button = UIButton()
//        button.setImage(UIImage(named: "\(imageName)"), forState: UIControlState.Normal)
//        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
//        button.sizeToFit()
//        return UIBarButtonItem(customView: button)
//    }
}